/* 
Set directory 
*/

    global root ENTER DIRECTORY
                                
	global data 		$root\Dta\Young Lives
	global temp			$root\Data\Temperature


use "$data\yls_all_data.dta", clear

merge 1:1 childid round using "$data\yls_date_temp.dta"

drop _merge


*Generating numeric ids
gen id = substr(childid,3,6)

destring id, replace

xtset id round

*Generating lagged test scores
foreach var of varlist n_raw_math n_raw_ppvt {
gen l`var' = l.`var'
}

*Dummies
tab round, gen(round_)
tab cohort, gen(cohort_)
tab distid, gen(dist_)
tab year, gen (year_)
tab month, gen (month_)
tab dayofweek, gen(dayofweek_)

egen monthid = concat(year month)
destring monthid, replace

tab monthid, gen(monthid_)

tab clustid, gen(clustid_)

*Standard errors: district-week
gen weekyear = week(idate)

egen weekid = concat(year round weekyear)
destring weekid, replace

egen distid_week = group(distid weekid)

local num 1
gen bin = 0 
foreach i of numlist 1/4{
replace bin = `num' if bin`i' == 1 
local num = `num' + 1
di `num'
}

local num 1
gen rain = 0 
foreach i of numlist 1/3{
replace rain = `num' if rain`i' == 1 
local num = `num' + 1
di `num'
}

ren humid rel_humid 
local num 1
gen humid = 0 
foreach i of numlist 1/4{
replace humid = `num' if humid`i' == 1 
local num = `num' + 1
di `num'
}

label define bindef 1 "<23C" 2 "23-25C" 3 "25-27C" 4 ">27C"
label val bin bindef

*Another dummy for eldest and a dummy for youngest
gen eldest_2 = 0
replace eldest_2 = 1 if ageorder == 1

gen youngest_2 = 0
replace youngest_2 = 1 if ageorder == 3


xtset id round

foreach var of varlist n_raw_math n_raw_ppvt ln_raw_math ln_raw_ppvt{
replace `var' = . if `var' == 0
}



xtset id round


*Balanced Panel
bys id: egen bal_math = count(n_raw_math)
bys id: egen bal_ppvt = count(n_raw_ppvt)
bys id: egen bal_english = count(n_raw_english)
bys id: egen bal_telugu = count(n_raw_telugu)

foreach var of varlist c_sleep c_chcare c_hhchore c_npaywork c_paywork c_school c_study c_play {
replace `var' = . if `var' == 77
replace `var' = `var'*60
bys id: egen bal_`var' = count(`var')
}


*L2
foreach var of varlist n_raw_math n_raw_ppvt {
gen l2`var' = l2.`var'
}

replace ln_raw_math = l2n_raw_math if ln_raw_math == . &  l2n_raw_math != .
replace ln_raw_ppvt = l2n_raw_ppvt if ln_raw_ppvt == . &  l2n_raw_ppvt != .

gen idays = .
replace idays = idays1 if round == 1
replace idays = idays2 if round == 2
replace idays = idays3 if round == 3
replace idays = idays4 if round == 4
replace idays = idays5 if round == 5

foreach num of numlist 1/4{
bys childid: replace mismatch_dates = mismatch_dates[_n+`num'] if mismatch_dates == .
}

gen dbin = 1 if bin2 == 1|bin3 == 1|bin4 == 1
replace dbin = 0 if bin1 == 1

gen dlbin = lbin2 + lbin3 + lbin4

*4 weeks before and after
foreach var of varlist l1temp l2temp l3temp l4temp l5temp l6temp l7temp l8temp l9temp l10temp ///
l11temp l12temp l13temp l14temp l15temp l16temp l17temp l18temp l19temp l20temp ///
l21temp l22temp l23temp l24temp l25temp l26temp l27temp l28temp l29temp l30temp {

gen a`var' = 1 if `var' >23
replace a`var' = 0 if a`var' == .

}

foreach var of varlist f1temp f2temp f3temp f4temp f5temp f6temp f7temp f8temp f9temp f10temp ///
f11temp f12temp f13temp f14temp f15temp f16temp f17temp f18temp f19temp f20temp ///
f21temp f22temp f23temp f24temp f25temp f26temp f27temp f28temp f29temp f30temp {

gen b`var' = 1 if `var' >23
replace b`var' = 0 if b`var' == .
}

egen a1week = rowtotal(al1temp - al7temp)
egen a2week = rowtotal(al8temp - al14temp)
egen a3week = rowtotal(al15temp - al21temp)
egen a4week = rowtotal(al22temp - al28temp)

egen b1week = rowtotal(bf1temp - bf7temp)
egen b2week = rowtotal(bf8temp - bf14temp)
egen b3week = rowtotal(bf15temp - bf21temp)
egen b4week = rowtotal(bf22temp - bf28temp)

*Humidity
foreach var of varlist l1humid l2humid l3humid l4humid l5humid l6humid l7humid l8humid l9humid l10humid ///
l11humid l12humid l13humid l14humid l15humid l16humid l17humid l18humid l19humid l20humid ///
l21humid l22humid l23humid l24humid l25humid l26humid l27humid l28humid l29humid l30humid {

gen a`var'1 = 1 if `var' >= 0 & `var' <=  90
gen a`var'2 = 1 if `var' > 90 & `var' <=  93
gen a`var'3 = 1 if `var' > 93 & `var' <=  96
gen a`var'4 = 1 if `var' > 96 

replace a`var'1 = 0 if a`var'1 == .
replace a`var'2 = 0 if a`var'2 == .
replace a`var'3 = 0 if a`var'3 == .
replace a`var'4 = 0 if a`var'4 == .
}

foreach var of varlist f1humid f2humid f3humid f4humid f5humid f6humid f7humid f8humid f9humid f10humid ///
f11humid f12humid f13humid f14humid f15humid f16humid f17humid f18humid f19humid f20humid ///
f21humid f22humid f23humid f24humid f25humid f26humid f27humid f28humid f29humid f30humid {

gen b`var'1 = 1 if `var' >= 0 & `var' <=  90
gen b`var'2 = 1 if `var' > 90 & `var' <=  93
gen b`var'3 = 1 if `var' > 93 & `var' <=  96
gen b`var'4 = 1 if `var' > 96 

replace b`var'1 = 0 if b`var'1 == .
replace b`var'2 = 0 if b`var'2 == .
replace b`var'3 = 0 if b`var'3 == .
replace b`var'4 = 0 if b`var'4 == .
}

foreach num of numlist 1/4{
egen ah1week`num' = rowtotal(al1humid`num' al2humid`num' al3humid`num' al4humid`num' al5humid`num' al6humid`num' al7humid`num')
egen ah2week`num' = rowtotal(al8humid`num' al9humid`num' al10humid`num' al11humid`num' al12humid`num' al13humid`num' al14humid`num')
egen ah3week`num' = rowtotal(al15humid`num' al16humid`num' al17humid`num' al18humid`num' al19humid`num' al20humid`num' al21humid`num')
egen ah4week`num' = rowtotal(al22humid`num' al23humid`num' al24humid`num' al25humid`num' al26humid`num' al27humid`num' al28humid`num')

egen bh1week`num' = rowtotal(bf1humid`num' bf2humid`num' bf3humid`num' bf4humid`num' bf5humid`num' bf6humid`num' bf7humid`num')
egen bh2week`num' = rowtotal(bf8humid`num' bf9humid`num' bf10humid`num' bf11humid`num' bf12humid`num' bf13humid`num' bf14humid`num')
egen bh3week`num' = rowtotal(bf15humid`num' bf16humid`num' bf17humid`num' bf18humid`num' bf19humid`num' bf20humid`num' bf21humid`num')
egen bh4week`num' = rowtotal(bf22humid`num' bf23humid`num' bf24humid`num' bf25humid`num' bf26humid`num' bf27humid`num' bf28humid`num')
}

*Rain
foreach var of varlist l1rain l2rain l3rain l4rain l5rain l6rain l7rain l8rain l9rain l10rain ///
l11rain l12rain l13rain l14rain l15rain l16rain l17rain l18rain l19rain l20rain ///
l21rain l22rain l23rain l24rain l25rain l26rain l27rain l28rain l29rain l30rain {

gen a`var'1 = 1 if `var' >= 0 & `var' <=  0.1854836
gen a`var'2 = 1 if `var' > 0.1854836 & `var' <=  2.799455
gen a`var'3 = 1 if `var' > 2.799455

replace a`var'1 = 0 if a`var'1 == .
replace a`var'2 = 0 if a`var'2 == .
replace a`var'3 = 0 if a`var'3 == .
}

foreach var of varlist f1rain f2rain f3rain f4rain f5rain f6rain f7rain f8rain f9rain f10rain ///
f11rain f12rain f13rain f14rain f15rain f16rain f17rain f18rain f19rain f20rain ///
f21rain f22rain f23rain f24rain f25rain f26rain f27rain f28rain f29rain f30rain {

gen b`var'1 = 1 if `var' >= 0 & `var' <=  0.1854836
gen b`var'2 = 1 if `var' > 0.1854836 & `var' <=  2.799455
gen b`var'3 = 1 if `var' > 2.799455

replace b`var'1 = 0 if b`var'1 == .
replace b`var'2 = 0 if b`var'2 == .
replace b`var'3 = 0 if b`var'3 == .

}

foreach num of numlist 1/3{
egen ar1week`num' = rowtotal(al1rain`num' al2rain`num' al3rain`num' al4rain`num' al5rain`num' al6rain`num' al7rain`num')
egen ar2week`num' = rowtotal(al8rain`num' al9rain`num' al10rain`num' al11rain`num' al12rain`num' al13rain`num' al14rain`num')
egen ar3week`num' = rowtotal(al15rain`num' al16rain`num' al17rain`num' al18rain`num' al19rain`num' al20rain`num' al21rain`num')
egen ar4week`num' = rowtotal(al22rain`num' al23rain`num' al24rain`num' al25rain`num' al26rain`num' al27rain`num' al28rain`num')

egen br1week`num' = rowtotal(bf1rain`num' bf2rain`num' bf3rain`num' bf4rain`num' bf5rain`num' bf6rain`num' bf7rain`num')
egen br2week`num' = rowtotal(bf8rain`num' bf9rain`num' bf10rain`num' bf11rain`num' bf12rain`num' bf13rain`num' bf14rain`num')
egen br3week`num' = rowtotal(bf15rain`num' bf16rain`num' bf17rain`num' bf18rain`num' bf19rain`num' bf20rain`num' bf21rain`num')
egen br4week`num' = rowtotal(bf22rain`num' bf23rain`num' bf24rain`num' bf25rain`num' bf26rain`num' bf27rain`num' bf28rain`num')
}


*Rain Vars*
gen ln_lavg_rain = ln(lavg_rain)
replace ln_lavg_rain = 0 if ln_lavg_rain == .

gen lavg_rainsq = lavg_rain^2

gen lavg_raincu = lavg_rain^3

gen ln_lavg_rainsq = ln(lavg_rainsq)
replace ln_lavg_rainsq = 0 if ln_lavg_rainsq == .


merge m:m distid year using "$data\yls_yearly_bins.dta"

keep if _merge == 3

drop _merge

merge 1:1 childid round using "$data\food.dta"

drop if _merge == 2

drop _merge

merge 1:1 childid round using "$data\attend.dta"

drop if _merge == 2



save "$data\small_temp_time.dta", replace






































